package com.tycmc.signelectroniccontracts.tools.images;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


/**
 * Created by Administrator on 2016/7/20 0020.
 */
public class ImageSetting {
    public static void urlImage(String url, ImageView imageView) {
        if (url.startsWith("http")) {
            netUrlImage(url, imageView);
        } else {
            fileUrlImage(url, imageView);
        }
    }

    public static void urlImage(String url, ImageView imageView, boolean iscircle) {
        if (url.startsWith("http")) {
            netUrlImage(url, imageView, iscircle);
        } else {
            fileUrlImage(url, imageView, iscircle);
        }
    }

    public static void urlImage(String url, ImageView imageView, ImageLoadingListener loadingListener) {
        if (url.startsWith("http")) {
            ImageLoader.getInstance().displayImage(url,imageView,loadingListener);
        } else {
            fileUrlImage(url, imageView);
        }
    }
    static void netUrlImage(String url, ImageView imageView) {
        netUrlImage(url, imageView, false);
    }

    static void netUrlImageScane(String url, ImageView imageView) {
        ImageSize imageSize = new ImageSize(150, 150);
        ImageLoader.getInstance().displayImage(url, imageView, imageSize);
    }

    static void netUrlImage(String url, ImageView imageView, boolean iscircle) {
        if (iscircle)
            ImageLoader.getInstance().displayImage(url, imageView, setRoundImage());
        else
            ImageLoader.getInstance().displayImage(url, imageView);
    }



    static void fileUrlImage(String url, ImageView imageView) {
        fileUrlImage(url, imageView, false);
    }

    static void fileUrlImage(String url, ImageView imageView, boolean iscircle) {
        if (!url.startsWith("file://")) {
            url = "file://" + url;
        }
        if (iscircle)
            ImageLoader.getInstance().displayImage(url, imageView, setRoundImage());
        else
            ImageLoader.getInstance().displayImage(url, imageView);
    }


    static DisplayImageOptions setRoundImage() {
        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
//        options.showImageOnLoading(R.drawable.headimg);
//        options.showImageForEmptyUri(R.drawable.default_image);
//        options.showImageOnFail(R.drawable.default_image);
        options.cacheInMemory(true);
        options.cacheOnDisk(true);
        options.considerExifParams(true);
        options.resetViewBeforeLoading(true);
        options.bitmapConfig(Bitmap.Config.RGB_565);
        options.displayer(new RoundedBitmapDisplayer(270));
        return options.build();
    }

    static DisplayImageOptions setRouImage() {
        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
//        options.showImageOnLoading(R.drawable.headimg);
//        options.showImageForEmptyUri(R.drawable.default_image);
//        options.showImageOnFail(R.drawable.default_image);
        options.cacheInMemory(true);
        options.cacheOnDisk(true);
        options.considerExifParams(true);
        options.resetViewBeforeLoading(true);
        options.bitmapConfig(Bitmap.Config.RGB_565);
        options.displayer(new RoundedBitmapDisplayer(25));
        return options.build();
    }

    public static void clean() {
        ImageLoader.getInstance().clearMemoryCache();
        ImageLoader.getInstance().clearDiskCache();
    }
    public static void init(Context context) {

        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
//        options.showImageOnLoading(R.drawable.img_def);
//        options.showImageForEmptyUri(R.mipmap.default_image);
//        options.showImageOnFail(R.mipmap.default_image);
        options.cacheInMemory(false);
        options.cacheOnDisk(true);
        options.considerExifParams(true);
        options.bitmapConfig(Bitmap.Config.RGB_565);
        options.imageScaleType(ImageScaleType.EXACTLY);
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.memoryCacheSize(2 * 1024 * 1024);
        config.diskCacheSize(50 * 1024 * 1024);
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.defaultDisplayImageOptions(options.build());
        ImageLoader.getInstance().init(config.build());
    }

}
